package calao.contact.client;

import java.util.Comparator;

import com.google.gwt.user.client.ui.Widget;

import calao.client.ServiceCallback;
import calao.client.ui.Form;
import calao.client.ui.FormListener;
import calao.client.ui.Format;
import calao.client.ui.HasLinks;
import calao.client.ui.HasTitle;
import calao.client.ui.Link;
import calao.client.ui.SortableGrid;

public class ContactTable 
extends SortableGrid 
implements HasTitle, FormListener, HasLinks
{

	class NameLink extends ContactLink {
		NameLink(Contact contact) {
			super(contact);
		}
		protected void show(ContactPanel panel) {
			panel.contactForm.addListener(ContactTable.this);
			mod.getDesktop().stackView(panel);
		}
	}

	class RefreshCallback extends ServiceCallback
	{
		protected void onReturn(Object obj) {
			contacts = (Contact[])obj;
			refresh();
		}		
	}

	final static int NAME_COL = 0;
	final static int PHONE_COL = 1;
	final static int FAX_COL = 2;
	final static int EMAIL_COL = 3;

	final ContactMod mod = ContactMod.instance;
	final Group group;
	final Organization org;

	Contact[] contacts;

	GroupList parentGroupList;

	private ContactTable(Group group, Organization org) 
	{
		super(4);
		this.group = group;
		this.org = org;
		setHeader(NAME_COL, mod.text.name());
		setHeader(PHONE_COL, mod.text.phone());
		setHeader(FAX_COL, mod.text.fax());
		setHeader(EMAIL_COL, mod.text.email());
		setColumnComparator(NAME_COL, new Comparator() {
			public int compare(Object o1, Object o2) {
				return ((Contact)o1).name.compareTo(((Contact)o2).name);
			}
		});
		setSortColumn(NAME_COL, false);
		setColumnFormat(NAME_COL, new Format() {
			public Widget formatAsWidget(Object obj) {
				return new NameLink((Contact)obj);
			}
		});
		setTitle();
		fetchAndRefresh();
	}
	
	void setTitle()
	{
		setTitle(group!=null? group.name : org.name);
	}

	public ContactTable(Group group)
	{
		this(group, null);
	}

	public ContactTable(Organization org)
	{
		this(null, org);
	}

	protected Object getData(int row, int col) 
	{
		Contact contact = contacts[row];
		switch (col) {
		case NAME_COL: return contact;
		case PHONE_COL: return contact.getPhone();
		case FAX_COL: return contact.getFax();
		case EMAIL_COL: return contact.getEmail();
		}
		return null;
	}

	protected int getRowCount() 
	{
		return contacts.length;
	}

	public void onChangesAvailable(Form form) { }

	public void onChangesCanceled(Form form) { }

	public void onChangesSaved(Form form) 
	{ 
		fetchAndRefresh();
	}

	void fetchAndRefresh()
	{
		if (group==null) mod.service.listContacts(-1, org.id, 2, new RefreshCallback());
		else mod.service.listContacts(group.id, -1, 2, new RefreshCallback());
	}

	public Link[] getLinks() 
	{
		if (group!=null && parentGroupList!=null) {
			return new Link[] { new Link(mod.text.editGroup()) {
				protected void onClick() {
					mod.getDesktop().stackView(new GroupForm(group, ContactTable.this));
				}
			}};
		}
		else return new Link[0];
	}
	
	void onGroupModified()
	{
		group.name = mod.getGroupName(group.id);
		setTitle();
		mod.getDesktop().refreshView(this);
		parentGroupList.refresh();
	}

}
